﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using CmdLine;
using Tedds_Tool;
using System.IO;
using Tedds_Tool.Compilers.LEET;
using Tedds_Tool.Automatables;
using Compiled_Tests;
using LEET.Exceptions.Models;

namespace GUI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        public static readonly string ProgramConfigPath = /*Build server hack. Ignore // */ (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).StartsWith("C:\\Windows\\system32")) ? "C:\\Users\\Hudson\\AppData\\Local\\LastActiveLeetProject.config" :
           Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\LEET\\LastActiveLeetProject.config";
        private static readonly string ProgramConfigDir = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).StartsWith("C:\\Windows\\system32")) ? "C:\\Users\\Hudson\\AppData\\Local\\" :
           Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\LEET\\";

        public App()
        {
            if (!Directory.Exists(ProgramConfigDir))
                Directory.CreateDirectory(ProgramConfigDir);
        }

        protected override void OnStartup(StartupEventArgs ev)
        {
            bool runWindow = true;
            LEETArguments args = new LEETArguments(ev.Args);

            Settings settings = new Settings();
            if (args.Options.ContainsKey("workspaceDir"))
                settings.ProjectRoot = new DirectoryInfo(args.Options["workspaceDir"]);
            else if (File.Exists(ProgramConfigPath))
            {
                String[] lastActiveProjectPath = File.ReadAllLines(ProgramConfigPath);
                if (lastActiveProjectPath.Length > 0)
                {
                    settings.ProjectRoot = new DirectoryInfo(lastActiveProjectPath[0]);
                }
            }

            if (args.Options.ContainsKey("dllPath"))
            {
                settings.TestDLL = new FileInfo(args.Options["dllPath"]);
            }
            else if (File.Exists(ProgramConfigPath))
            {
                String[] lastActiveProjectPath = File.ReadAllLines(ProgramConfigPath);
                if (lastActiveProjectPath.Length == 2)
                {
                    settings.TestDLL = new FileInfo(lastActiveProjectPath[1]);
                }
            }
            
            if (args.Arguments.Contains("recompileAll"))
            {
                // run compiler over LEET Tests scripts
                IScriptFinder finder = new ScriptFinder(settings);
                IEnumerable<String> scripts = finder.ListTestScripts();
                foreach (String script in scripts)
                {
                    try
                    {
                        LEETFactory LeetFactory = new LEETFactory(new Box(), finder);
                        Facade facade = new Facade(new Compiler(new MSTestAssertionSet(), LeetFactory, finder), null, LeetFactory, finder);
                        facade.CompileAsTest(script);
                        Console.WriteLine("Compile of " + script + " succeeded.");
                    }
                    catch (InvalidScriptException error)
                    {
                        Console.WriteLine("\tFailed to Compile " + script + ": " + error.Message);
                    }
                    catch (InvalidInstructionException error)
                    {
                        Console.WriteLine("\tFailed to Compile " + script + ": " + error.Message);
                    }
                }
                // and exit
                Shutdown(0);
                runWindow = false;
            }


            if (runWindow)
            {
                Window1 win = new Window1();
                win.settings = settings;
                win.ReloadScriptInfo(null, null);
                MainWindow = win;
                MainWindow.Show();
            }
        }


    }
}
